﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LevenBlog.Core.Data;
using LevenBlog.Core.Entities;
using LevenBlog.Core.Cache;

namespace LevenBlog.Core.Service {
    public static class FriendLink {
        private static readonly string cacheName = "levenblog_servicecache_frindlink";

        public static int Count(IDbProvider provider) {
            return ProviderManager.Factory.FriendLink.Count(provider);
        }

        public static void Insert(IDbProvider provider, lb_friendlink link) {
            ProviderManager.Factory.FriendLink.Insert(provider, link);
            Remove();
        }

        public static void Update(IDbProvider provider, lb_friendlink link) {
            ProviderManager.Factory.FriendLink.Update(provider, link);
            Remove();
        }

        public static void Delete(IDbProvider provider, int id) {
            ProviderManager.Factory.FriendLink.Delete(provider, id);
            Remove();
        }

        public static IList<lb_friendlink> Get(IDbProvider provider) {
            IList<lb_friendlink> find = ObjectCache.Get<IList<lb_friendlink>>(cacheName);
            if (find == null) {
                find = ProviderManager.Factory.FriendLink.Get(provider);
                ObjectCache.Insert(cacheName, find);
            }
            return find;
        }

        public static lb_friendlink Get(IDbProvider provider, int id) {
            return Get(provider).FirstOrDefault(l => l.id == id);
        }

        public static IList<lb_friendlink> GetFromDatabase(IDbProvider provider, FriendLinkState selectState) {
            return ProviderManager.Factory.FriendLink.Get(provider, selectState);
        }

        public static void Remove() {
            ObjectCache.Remove(cacheName);
        }
    }
}
